import type { App, AppContext } from 'vue'
import type {
  MessageOptions,
  MessageParamsWithType,
} from 'element-plus'
import {
  ElMessage,
  ElMessageBox,
  ElNotification,
} from 'element-plus'
import Axios from '@/api/ajax'

const { getPower, getPowerByIcon } = usePagePower()
function handleMessage(type: MessageOptions['type']) {
  return (
    options?: MessageParamsWithType,
    appContext?: AppContext | null | undefined,
  ) => {
    const opt: MessageOptions = { plain: true, type }
    if (typeof options === 'string') {
      opt.message = options
    }
    else {
      Object.assign(opt, options)
    }
    return ElMessage(opt, appContext)
  }
}
['success', 'warning', 'info', 'error'].forEach(
  (type: string) => {
    (ElMessage as any)[type] = handleMessage(
      type as MessageOptions['type'],
    )
  },
)

export default (app: App<Element>) => {
  const { globalProperties } = app.config
  globalProperties.$http = Axios
  globalProperties.$message = ElMessage
  globalProperties.$notify = ElNotification
  globalProperties.$msgbox = ElMessageBox
  globalProperties.$alert = ElMessageBox.alert
  globalProperties.$confirm = ElMessageBox.confirm
  globalProperties.$prompt = ElMessageBox.prompt
  globalProperties.$power = getPower
  globalProperties.$powerByIcon = getPowerByIcon
}
